//
//  TopicVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/31.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "TopicVC.h"
#import "FindViewCell_thrid.h"
#import "ZTXQViewController.h"
#import "IGTopicListApi.h"

@interface TopicVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *collectionV;
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, strong) IGTopicListApi *api;
@property (nonatomic, strong) NSMutableArray *topicList;
@end

@implementation TopicVC

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"专题";
    [self ctreatCollectionView];
    self.title = NSLocalizedString(@"Feature", nil);
    
    self.api = [IGTopicListApi new];
    _pageSize = @"10";
    _pageNum = @"1";
    self.topicList = [@[] mutableCopy];
    [self dataRequest];
}

- (void)dataRequest
{
    self.api.pageSize = _pageSize;
    self.api.pageNum = _pageNum;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGTopicListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.collectionV ig_endHeaderRefreshing];
        [self.collectionV ig_endFooterRefreshing];
        if (responseObject)
        {
            IGTopicListResp *resp = responseObject;
            IGTopicListModel *model = resp.data;
            if (model.list && model.list.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.collectionV ig_endFooterRefreshingWithNoMoreData];
            }
            if ([_pageNum isEqualToString:@"1"])
            {
                _topicList = [NSMutableArray arrayWithArray:model.list];
            }
            else
            {
                [_topicList addObjectsFromArray:model.list];
            }
            
            [selfWeak.collectionV reloadData];
        }
        else
        {
            
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.collectionV ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak dataRequest];
    }];
}

- (void)ctreatCollectionView
{
    @KWeakObj(self)
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionV.backgroundColor = [UIColor c2Color];
    self.collectionV.alwaysBounceVertical = YES;
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FindViewCell_thrid" bundle:nil] forCellWithReuseIdentifier:@"FindViewCell_thrid"];
    [self.collectionV ig_setHeaderWithRefreshingBlock:^{
        _pageSize = @"10";
        _pageNum = @"1";
        [selfWeak dataRequest];
    }];
    [self.view addSubview:self.collectionV];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _topicList.count ?:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_topicList.count)
    {
        FindViewCell_thrid *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FindViewCell_thrid" forIndexPath:indexPath];
        NSDictionary *dic = _topicList[indexPath.item];
        if (dic)
        {
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
            cell.titleL.text = dic[@"title"];
            cell.subL.text = dic[@"subTitle"];
        }
        return cell;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KSCREENWIDTH ,(KSCREENWIDTH - 30) * 9 / 16 + 95);
}

//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = _topicList[indexPath.item];
    ZTXQViewController *vc = [[ZTXQViewController alloc] init];
    vc.s_id = dic[@"id"];
    [UIViewController ig_pushViewController:vc animated:YES];
}

@end
